<template>
	<view class="free-to-buy" v-if="detail.img">
		<!-- 背景图 -->
		<view class="free-to-bg"></view>
		<!-- 活动规则 -->
		<view class="active-gz" @click.stop="openRules">活动规则</view>
		<!-- 顶部 -->
		<view class="free-top-box">
			<view class="active-goods-img" @click="bindActiveDeatil()"><image :src="IMG_URL + detail.img" mode="aspectFill"></image></view>
			<view class="active-goods-time">
				<uni-countdown
					v-if="timeShow"
					splitorColor="#fa8384"
					color="#FFFFFF"
					background-color="#fa8384"
					:day="times[0]"
					:hour="times[1]"
					:minute="times[2]"
					:second="times[3]"
				></uni-countdown>
				后结束
			</view>
			<view class="active-goods-popNum">
				<text class="popNum" v-if="detail.left_fans != 0">已邀请{{ detail.fans }}人，还差{{ detail.left_fans }}人“0”元购</text>
				<text class="popNum" v-else>恭喜你！“0”元获得一只猫咪</text>
			</view>
			<view class="active-goods-jdt-box" v-if="detail.left_fans != 0"><view class="active-goods-jdt" :style="{ width: jdtWidth }"></view></view>
			<block v-if="detail.left_fans != 0">
				<button class="active-goods-fx" v-if="detail.if_timeover == 2">活动已经结束啦~</button>
				<button class="active-goods-fx" v-if="detail.if_taken == 1">已有人获得该猫咪~</button>
				<button class="active-goods-fx" open-type="share">{{ detail.fans === 0 ? '还未分享，快去邀请好友吧~' : '继续分享' }}</button>
			</block>
			<button class="active-goods-fx" v-else>截图联系客服获取</button>
		</view>
		<!-- 更多活动 -->
		<view class="free-more-active">
			<view class="free-more-active-text">更多精彩活动等你来</view>
			<image class="free-more-active-img" :src="haibao.img" mode="" @click="bindWebView(haibao.url)"></image>
			<view class="free-more-active-box">
				<view class="free-more-active-title">
					<text>猫咪不够看</text>
					<text @click="bindShouYe">查看更多</text>
				</view>
				<view class="free-more-active-list">
					<view class="free-more-active-item" v-for="(item, index) in moreCatList" :key="index" @click="bindBuyDeatil(item.id)">
						<image :src="IMG_URL + item.img" mode="aspectFill"></image>
					</view>
				</view>
			</view>
		</view>
		<!-- 活动规则 -->
		<activity-rules-popup ref="activityRulesPopup" />
	</view>
</template>

<script>
const app = getApp();
import activityRulesPopup from './components/activity-rules-popup/index.vue';
import { freeCatOne, xMiaosha, moreCat } from '@/api/active.js';
import { xShare } from '@/api/index.js';
import countDown from './countDown.js';
export default {
	components: {
		activityRulesPopup
	},
	data() {
		return {
			id: '',
			IMG_URL: app.globalData.IMG_URL,
			detail: {}, // 顶部详情
			haibao: {}, //海报信息
			moreCatList: [], //更多猫咪信息
			timeShow: true,
			times: [], //倒计时
			jdtWidth: 0
		};
	},
	onLoad(options) {
		this.id = options.id;
		if (app.globalData.openid) {
			this.freeCatOne(); // 获取活动详情
			this.xShare(options.openid);
		} else {
			app.getOpenId().then(res => {
				this.freeCatOne();
				this.xShare(options.openid);
			});
		}
		this.xMiaosha();
		this.moreCat();
	},
	methods: {
		// 获取活动详情
		async freeCatOne() {
			const res = await freeCatOne({
				openid: app.globalData.openid,
				id: this.id
			});
			this.jdtWidth = `${parseInt((res.fans / (res.fans + res.left_fans)) * 100)}%`;
			this.detail = res;
			this.times = countDown(res.left_time * 1000);
			this.timeShow = true;
			if (res.if_timeover == 2 || res.if_taken == 1) {
				uni.hideShareMenu();
			}
		},
		// 获取海报
		async xMiaosha() {
			const res = await xMiaosha();
			this.haibao = res;
		},
		// 更多猫咪
		async moreCat() {
			const res = await moreCat();
			this.moreCatList = res;
		},
		// 分享接口
		async xShare(openid) {
			if (openid) {
				const res = await xShare({
					type: 7,
					goods_id: this.id,
					openid: openid,
					openid_in: app.globalData.openid
				});
			}
		},
		// 查看活动规则
		openRules() {
			this.$refs.activityRulesPopup.open();
		},
		// 跳转webview
		bindWebView(url) {
			uni.navigateTo({
				url: `/pages/webView/webView?url=${url}`
			});
		},
		// 跳转活动详情
		bindActiveDeatil() {
			uni.navigateTo({
				url: `/pages/buyDetail/buyDetail?id=${this.id}&type=4&active=active`
			});
		},
		// 跳转卖猫详情
		bindBuyDeatil(id) {
			uni.navigateTo({
				url: `/pages/buyDetail/buyDetail?id=${id}&type=4`
			});
		},
		// 查看更多
		bindShouYe() {
			uni.reLaunch({
				url: '/pages/index/index'
			});
		}
	},
	// 分享
	onShareAppMessage() {
		return {
			title: '0元购',
			path: `/pages/freeToBuy/freeToBuyDetail?openid=${app.globalData.openid}&id=${this.id}`
		};
	}
};
</script>

<style lang="scss">
page {
	height: 100%;
	width: 100%;
	background: #fa8384;
	background-attachment: fixed;
	background-size: cover;
}
.free-to-bg {
	position: fixed;
	top: 56rpx;
	right: 0;
	left: 0;
	width: 750rpx;
	height: 1100rpx;
	background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/acat-xcx/lh-huodong-bg.png) no-repeat;
	background-size: cover;
	z-index: -1;
}
.free-to-buy {
	position: relative;
	padding: 30rpx 30rpx 40rpx;
	box-sizing: border-box;
	.active-gz {
		position: absolute;
		top: 54rpx;
		left: 0;
		width: 124rpx;
		height: 48rpx;
		background: #fa8384;
		border-radius: 0 28rpx 28rpx 0;
		text-align: center;
		line-height: 48rpx;
		font-size: 24rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #ffffff;
	}
	.free-top-box {
		background: #ffffff;
		border-radius: 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-bottom: 40rpx;
		margin-bottom: 30rpx;
		.active-goods-img {
			width: 100%;
			height: 360rpx;
			margin-bottom: 32rpx;
			& > image {
				width: 100%;
				height: 100%;
				border-radius: 8rpx 8rpx 0px 0px;
			}
		}
		.active-goods-time {
			font-size: 24rpx;
			font-weight: 400;
			color: #fa8384;
			letter-spacing: 2rpx;
			display: flex;
			align-items: center;
		}
		/* 
		 <view class="active-goods-jdt-box">
		 	<view class="active-goods-jdt"></view>
		 </view>
		 */
		.active-goods-popNum {
			font-size: 32rpx;
			font-family: PingFangSC-Semibold, PingFang SC;
			font-weight: 600;
			color: #333333;
			margin-top: 16rpx;
		}
		.active-goods-jdt-box {
			width: 630rpx;
			height: 16rpx;
			background: #ffebeb;
			border-radius: 10rpx;
			margin-top: 32rpx;
			.active-goods-jdt {
				width: 20%;
				height: 100%;
				border-radius: 10rpx;
				background: linear-gradient(90deg, #fdb9ba 0%, #fa8384 100%);
			}
		}
		.active-goods-fx {
			width: 630rpx;
			height: 72rpx;
			line-height: 72rpx;
			background: #fa8384;
			border-radius: 8rpx;
			color: #ffffff;
			font-size: 32rpx;
			font-weight: 600;
			margin-top: 32rpx;
		}
	}
	.free-more-active {
		padding: 24rpx 30rpx 40rpx;
		background: #ffffff;
		border-radius: 8rpx;
		box-sizing: border-box;
		.free-more-active-text {
			font-size: 32rpx;
			font-weight: 600;
			color: #fa8384;
			text-align: center;
			margin-bottom: 20rpx;
			position: relative;
			&::after {
				content: '';
				width: 44rpx;
				height: 24rpx;
				background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/acat-xcx/lh-huodong-left.png) no-repeat;
				background-size: cover;
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				left: 114rpx;
			}
			&::before {
				content: '';
				width: 44rpx;
				height: 24rpx;
				background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/acat-xcx/lh-huodong-right.png) no-repeat;
				background-size: cover;
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				right: 114rpx;
			}
		}
		.free-more-active-img {
			width: 100%;
			height: 178rpx;
			margin-bottom: 30rpx;
		}
		.free-more-active-box {
			.free-more-active-title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 30rpx;
				& > text:nth-of-type(1) {
					font-size: 32rpx;
					font-weight: 600;
					color: #333333;
				}
				& > text:nth-of-type(2) {
					font-size: 24rpx;
					color: #999999;
					padding-right: 30rpx;
					position: relative;
					&::before {
						content: '';
						width: 28rpx;
						height: 28rpx;
						background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/acat-xcx/lh-huodong-zuob.png) no-repeat;
						background-size: cover;
						position: absolute;
						top: 50%;
						transform: translateY(-50%);
						right: 0;
					}
				}
			}
			.free-more-active-list {
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				.free-more-active-item {
					width: 300rpx;
					height: 300rpx;
					margin-bottom: 30rpx;
					& > image {
						width: 100%;
						height: 100%;
						border-radius: 8rpx;
					}
					&:nth-last-of-type(1) {
						margin-bottom: 0;
					}
					&:nth-last-of-type(2) {
						margin-bottom: 0;
					}
				}
			}
		}
	}
}
/deep/.uni-countdown__number {
	width: 36rpx !important;
	height: 36rpx !important;
	border-radius: 4rpx !important;
	text-align: center !important;
	line-height: 36rpx !important;
	font-size: 24rpx !important;
}
/deep/.uni-countdown__splitor {
	padding: 0 4rpx !important;
}
</style>
